package com.ruoyi.web.service.Impl;

import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.pinyin.PinYinUtils;
import com.ruoyi.web.domain.mes.MesWastesettle;
import com.ruoyi.web.domain.vo.CareDetailVo;
import com.ruoyi.web.domain.vo.WasteVo;
import com.ruoyi.web.mapper.MesConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.web.domain.mes.MesWasteDetail;
import com.ruoyi.web.mapper.MesWasteMapper;
import com.ruoyi.web.domain.mes.MesWaste;
import com.ruoyi.web.service.IMesWasteService;

import javax.annotation.Resource;

/**
 * 废料处理Service业务层处理
 *
 * @author levicash
 * @date 2024-01-23
 */
@Service
public class MesWasteServiceImpl implements IMesWasteService {
    @Resource
    private MesWasteMapper mesWasteMapper;

    @Resource
    private MesConfigMapper configMapper;

    /**
     * 查询废料处理
     *
     * @param id 废料处理主键
     * @return 废料处理
     */
    @Override
    public MesWaste selectMesWasteById(Long id) {
        return mesWasteMapper.selectMesWasteById(id);
    }

    /**
     * 查询废料处理列表
     *
     * @param mesWaste 废料处理
     * @return 废料处理
     */
    @Override
    public List<MesWaste> selectMesWasteList(MesWaste mesWaste) {
        return mesWasteMapper.selectMesWasteList(mesWaste);
    }

    @Override
    public List<MesWaste> selectMesWasteListJS(MesWaste mesWaste) {
        return mesWasteMapper.selectMesWasteListJS(mesWaste);
    }

    /**
     * 新增废料处理
     *
     * @param mesWaste 废料处理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMesWaste(MesWaste mesWaste) {
        mesWaste.setCreateBy(SecurityUtils.getUsername());
        mesWaste.setCreateTime(DateUtils.getNowDate());

        // 生成废料处理单编号
        Map<String, Object> map = new HashMap<>();
        map.put("columnName", "code");
        map.put("tableName", "mes_waste");
        int maxNum = configMapper.getMaxId(map);
        String code = PinYinUtils.getCodeNormal(maxNum, "FLCLD");
        mesWaste.setCode(code);

        int rows = mesWasteMapper.insertMesWaste(mesWaste);
        insertMesWasteDetail(mesWaste);
        return rows;
    }

    /**
     * 修改废料处理
     *
     * @param mesWaste 废料处理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMesWaste(MesWaste mesWaste) {
        mesWaste.setUpdateBy(SecurityUtils.getUsername());
        mesWaste.setUpdateTime(DateUtils.getNowDate());
        int status = mesWaste.getStatus();
        if (status == 1) {// 提交

        } else {
            mesWasteMapper.deleteMesWasteDetailByWasteCode(mesWaste.getCode());
            insertMesWasteDetail(mesWaste);
        }
        return mesWasteMapper.updateMesWaste(mesWaste);
    }

    /**
     * 批量删除废料处理
     *
     * @param ids 需要删除的废料处理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesWasteByIds(Long[] ids) {
        List<String> codes = new ArrayList<>();
        List<MesWaste> list = mesWasteMapper.selectMesWasteByIds(ids);
        if (list.size() > 0) {
            for (MesWaste item : list) {
                codes.add(item.getCode());
            }
            String array[] = codes.toArray(new String[0]);
            mesWasteMapper.deleteMesWasteDetailByWasteCodes(array);
        }
        return mesWasteMapper.deleteMesWasteByIds(ids);
    }

    /**
     * 删除废料处理信息
     *
     * @param id 废料处理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesWasteById(Long id) {
        MesWaste waste = mesWasteMapper.selectMesWasteById(id);
        mesWasteMapper.deleteMesWasteDetailByWasteCode(waste.getCode());
        return mesWasteMapper.deleteMesWasteById(id);
    }

    /**
     * 新增废料处理明细信息
     *
     * @param mesWaste 废料处理对象
     */
    public void insertMesWasteDetail(MesWaste mesWaste) {
        List<MesWasteDetail> mesWasteDetailList = mesWaste.getMesWasteDetailList();
        String wasteCode = mesWaste.getCode();
        if (StringUtils.isNotNull(mesWasteDetailList)) {
            List<MesWasteDetail> list = new ArrayList<MesWasteDetail>();
            for (MesWasteDetail mesWasteDetail : mesWasteDetailList) {
                mesWasteDetail.setUpdateBy(SecurityUtils.getUsername());
                mesWasteDetail.setUpdateTime(DateUtils.getNowDate());
                mesWasteDetail.setWasteCode(wasteCode);
                list.add(mesWasteDetail);
            }
            if (list.size() > 0) {
                mesWasteMapper.batchMesWasteDetail(list);
            }
        }
    }

    /**
     * 查询废料处理明细
     *
     * @param map 查询参数
     * @return 废料处理关联集合
     */
    @Override
    public List<WasteVo> selectMesWasteDetailList(Map<String, Object> map) {
        return mesWasteMapper.selectMesWasteDetailList(map);
    }

}
